﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace DE.Sdc.SharePoint.SPContentStructure.Common
{
    public class HierarchicalElement<TParent> : IdentifiedElement, IHierarchicalElement where TParent : IHierarchicalElement
    {
        ActionType _action;
        bool _recursive;
        
        [XmlIgnore]
        public TParent Parent { get; set; }

        [XmlIgnore]
        IHierarchicalElement IHierarchicalElement.Parent
        {
            get
            {
                return this.Parent;
            }
            set
            {
                this.Parent = (TParent) value;
            }
        }
        
        [XmlAttribute]
        public bool Recursive
        {
            get
            {
                return (Parent != null && Parent.Recursive) ? Parent.Recursive : _recursive;
            }
            set
            {
                _recursive = value;
            }
        }

        [XmlAttribute]
        public override ActionType Action
        {
            get
            {
                bool isnull = Parent != null;

                if (Parent != null && Parent.Recursive)
                {
                    return Parent.Action;
                }
                else
                {
                    return _action;
                }
                //return (Parent != null && Parent.Recursive) ? Parent.Action : Action;
            }
            set
            {
                _action = value;
            }
        }

        [XmlIgnore]
        public object ImportDefinitionObject { get; set; }
    } 
}
